package com.liush.srb.core.service.impl;

import com.liush.srb.core.pojo.entity.Lend;
import com.liush.srb.core.pojo.entity.LendItem;
import com.liush.srb.core.pojo.entity.LendItemReturn;
import com.liush.srb.core.mapper.LendItemReturnMapper;
import com.liush.srb.core.service.LendItemReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liush.srb.core.util.Amount1Helper;
import com.liush.srb.core.util.Amount2Helper;
import com.liush.srb.core.util.Amount3Helper;
import com.liush.srb.core.util.Amount4Helper;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author liush
 * @since 2023-03-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    @Override
    public List<LendItemReturn> saveLendItemReturns(Lend lend, List<LendItem> lendItems, BigDecimal yearRate, Integer period, Integer returnMethod, Map<Integer, Long> lendReturnPeriodId) {
        List<LendItemReturn> lendItemReturns = new ArrayList<>();
        lendItems.forEach(lendItem -> {//遍历所有的投资记录
            //获取本次投资记录每期回款本金/利息的map
            BigDecimal lendItemInvestAmount = lendItem.getInvestAmount();
            Map<String, Map<Integer, BigDecimal>> map2 = new HashMap<>();
            this.initMap(map2, returnMethod,
                    period, lendItemInvestAmount, yearRate);
            Map<Integer, BigDecimal> lendItemPerMonthPrincipal = map2.get("perMonthPrincipal");
            Map<Integer, BigDecimal> lendItemPerMonthInterest = map2.get("perMonthInterest");
            for (Integer i = 1; i <= period; i++) {//为正在遍历的投资记录创建回款计划
                LendItemReturn lendItemReturn = new LendItemReturn();
                lendItemReturn.setLendReturnId(lendReturnPeriodId.get(i));//当前回款计划对应的还款计划的id
                lendItemReturn.setLendItemId(lendItem.getId());//回款计划的 投资记录id
                lendItemReturn.setLendId(lend.getId());
                lendItemReturn.setInvestUserId(lendItem.getInvestUserId());//投资记录 投资人id
                lendItemReturn.setInvestAmount(lendItem.getInvestAmount());
                lendItemReturn.setCurrentPeriod(i);
                lendItemReturn.setLendYearRate(lend.getLendYearRate());
                lendItemReturn.setReturnMethod(lend.getReturnMethod());
                // lendReturn得到的map中计算的每期本金利息 使用的是lend的investAmout计算的
                //此处应该用 投资记录的投资金额来计算，并且每个投资记录投资金额不一定一样 都需要额外的获取map
                //本金：
                if (lend.getReturnMethod() == 4) {
                    if (i == period) {
                        lendItemReturn.setPrincipal(lendItemPerMonthPrincipal.get(1));
                        lendItemReturn.setInterest(Amount4Helper.getInterestCount(lendItemInvestAmount, yearRate,
                                period));
                        lendItemReturn.setTotal(lendItemReturn.getPrincipal().add(lendItemReturn.getInterest()));
                    } else {
                        lendItemReturn.setPrincipal(new BigDecimal(0));
                        lendItemReturn.setInterest(new BigDecimal(0));
                        lendItemReturn.setTotal(new BigDecimal(0));
                    }
                } else {
                    lendItemReturn.setPrincipal(lendItemPerMonthPrincipal.get(i));
                    lendItemReturn.setInterest(lendItemPerMonthInterest.get(i));
                    lendItemReturn.setTotal(lendItemReturn.getPrincipal().add(lendItemReturn.getInterest()));
                }
                //手续费
                lendItemReturn.setFee(null);
                //回款日期
                lendItemReturn.setReturnDate(new DateTime(lend.getLendStartDate())
                        .plusMonths(i).toDate());
                lendItemReturn.setRealReturnTime(null);
                lendItemReturn.setOverdue(false);
                lendItemReturn.setOverdueTotal(new BigDecimal(0));
                lendItemReturn.setStatus(0);
                lendItemReturns.add(lendItemReturn);
            }
        });
        //批量保存
        this.saveBatch(lendItemReturns);
        return lendItemReturns;
    }

    public void initMap(Map<String, Map<Integer, BigDecimal>> map, Integer returnMethod, Integer period,
                        BigDecimal lendItemInvestAmount, BigDecimal yearRate) {
        Map<Integer, BigDecimal> perMonthPrincipal, perMonthInterest;
        switch (returnMethod) {
            case 1:
                perMonthPrincipal = Amount1Helper.getPerMonthPrincipal(lendItemInvestAmount,
                        yearRate, period);
                perMonthInterest = Amount1Helper.getPerMonthInterest(lendItemInvestAmount,
                        yearRate, period);
                break;
            case 2:
                perMonthPrincipal = Amount2Helper.getPerMonthPrincipal(lendItemInvestAmount,
                        yearRate, period);
                perMonthInterest = Amount2Helper.getPerMonthInterest(lendItemInvestAmount,
                        yearRate, period);
                break;
            case 3:
                perMonthPrincipal = Amount3Helper.getPerMonthPrincipal(lendItemInvestAmount,
                        yearRate, period);
                perMonthInterest = Amount3Helper.getPerMonthInterest(lendItemInvestAmount,
                        yearRate, period);
                break;
            default:
                perMonthPrincipal = Amount4Helper.getPerMonthPrincipal(lendItemInvestAmount,
                        yearRate, period);
                perMonthInterest = Amount4Helper.getPerMonthInterest(lendItemInvestAmount,
                        yearRate, period);
                break;
        }
        map.put("perMonthPrincipal", perMonthPrincipal);
        map.put("perMonthInterest", perMonthInterest);
    }
}
